<?php

namespace Admin\Controller;

use Core\Controller\AdminBaseController;
use Core\Exception\BusinessException;
use Core\Helper\ApplicationDirectoryHelper;
use Exception;
use Zend\Json\Json;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;

class CommonController extends AdminBaseController {

    public function getControllerByModuleAction(){
        try{
            $module = $this->params()->fromPost('mymodule');
            $result = ApplicationDirectoryHelper::getAllControllerByModule($module);
            return new JsonModel(array('data'=>$result,'success'=>true));
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    public function lovArticleAction(){
        try{
            $articleTable = $this->getTableFinder()->getArticleTable();
            $articleTable->getAllActiveArticle();

            /*$article = new Article();
            $allArticles = $article->getAllActiveArticle();

            $tmpArr = array();
            foreach ($allArticles as $aArticle) {
                array_push($tmpArr, array('id'=>$aArticle->getParameter()
                , 'name'=>$aArticle->getSpecification()));
            }

            $this->_helper->json(array('data'=>$tmpArr));*/
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    public function loadDistrictAction(){
        try{
            $city = $this->params()->fromPost("city");
            if(!$city){
                throw new BusinessException("Chưa truyền id để xóa ");
            }
            $districtTable = $this->getTableFinder()->getDistrictTable();
            $allDistrict = $districtTable->getAllByProvince($city);
            return new JsonModel(array('success'=>true, 'data'=>$allDistrict));
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }
}